<!--
@name:dialogTestItems
@author:Zhenglei
@date:16:51
@description:dialogTestItems
@update:2022/8/916:51
-->
<template>
  <el-dialog :title="title"
             :visible.sync="visible"
             :before-close="modalClose"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="50%">
    <el-form :model="subData"
             label-position="right"
             label-width="150px"
             ref="formModelRef"
             :rules='formModelRules'>
      <el-form-item label="检测项：" prop="detectId" :rules="isEdit?[{required:false}]:formModelRules.detectId">
        <el-select v-model="subData.detectId"
                   filterable
                   class="w-300"
                   value-key="id"
                   :disabled="isEdit"
                   @change="handleChangeDetect"
                   :placeholder="placeholderDetect">
          <el-option v-for="item in detectDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检测项指标：">
        <el-select v-model="subData.targetId"
                   filterable
                   value-key="id"
                   @change="handleChangeTarget"
                   :disabled="isEdit"
                   class="w-300"
                   :placeholder="placeholderTarget">
          <el-option v-for="item in targetDataSource"
                     :key="item.id"
                     :label="item.name"
                     :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="结论：">
        <el-select v-model="subData.relationId"
                   filterable
                   :disabled="isEdit"
                   class="w-300"
                   multiple
                   :placeholder="placeholderEnding">
          <el-option v-for="item in endingDataSource"
                     :key="item"
                     :label="item"
                     :value="item">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="健康建议：">
        <vue-neditor-wrap :destroy="true"
                          v-model="subData.suggest"
                          :config="myConfig"></vue-neditor-wrap>
      </el-form-item>


    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleSave()">保 存
      </el-button>
    </p>

  </el-dialog>

</template>

<script>
import VueNeditorWrap from "vue-neditor-wrap";

export default {
  name: "dialogTestItems",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      myConfig: {
        // 你的UEditor资源存放的路径,相对于打包后的index.html
        UEDITOR_HOME_URL: this.api.uEditorHomeUrl,
        // 编辑器不自动被内容撑高
        autoHeightEnabled: false,
        // 初始容器高度
        initialFrameHeight: '418',
        // 初始容器宽度
        initialFrameWidth: '100%',
        // 关闭自动保存
        enableAutoSave: false,
        scaleEnabled: true
      },
      title: '新 增',
      subData: {
        id: null,
        detectId: '',
        targetId: '',
        relationId: [],
        suggest: '',
        type: 20,
      },
      resultId:'',
      isEdit: false,
      placeholderDetect: '请选择检测项',
      placeholderTarget: '请选择检测项指标',
      placeholderEnding: '请选择结论',
      detectDataSource: [],
      targetDataSource: [],
      endingDataSource: [],
      formModelRules: {
        detectId: [{required: true, message: '请选择检测项', trigger: 'blur'},],
      }

    }
  },
  created() {
    if (this.dataObj.id) {
      this.title = '编 辑'
      this.isEdit = true
      this.placeholderDetect = this.dataObj.deviceName
      if (this.dataObj.referenceName){
        this.placeholderTarget = this.dataObj.referenceName
      }
      if (this.dataObj.relationName) {
        this.placeholderEnding = this.dataObj.relationName
      }
      if (this.dataObj.suggest) {
        this.subData.suggest = this.dataObj.suggest
      }
    } else {
      this.isEdit = false
      this.title = '新 增'
    }
    this.getDetectList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false);
    },
    //选择病种
    handleChangeDetect(val) {
      this.subData.targetId = ''
      this.subData.relationId = []
      this.targetDataSource = val.detectionIndexReferences

    },
    //选择指标
    handleChangeTarget(val) {
      this.getReferenceInfo(this.subData.detectId.id,val.id)
    },
    //查询检测项
    getDetectList() {
      this.post(this.api.healthMap.getDetectList).then((res) => {
        if (res.data.code === '200') {
          this.detectDataSource = res.data.object
        }
      })
    },
    //检测项查询结论
    getReferenceInfo(deviceId,referenceId) {
      this.post(this.api.healthMap.getReferenceInfo, {
        deviceId: deviceId,
        referenceId:referenceId
      }).then((res) => {
        if (res.data.code === '200') {
          this.endingDataSource = res.data.object[0].resultSet
          this.resultId = res.data.object[0].id
        }
      })
    },
    handleSave() {
      this.$refs.formModelRef.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            //编辑
            this.post(this.api.healthMap.updateSuggest, {
              id: this.dataObj.id,
              suggest: this.subData.suggest
            }).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('更新成功!');
                this.$parent.getList();
                this.modalClose();
              }
            })
          } else {
            //新增
            let arr = []
            if (this.subData.relationId.length === 0 && this.subData.targetId.id) {
              //选择了检测指标
              arr.push({
                type: 41,
                relationId: this.subData.targetId.deviceReferenceId,
                suggest: this.subData.suggest
              })
            } else if (this.subData.relationId.length === 0 && !this.subData.targetId.id) {
              //选择了检测项
              arr.push({
                type: 40,
                relationId: this.subData.detectId.id,
                suggest: this.subData.suggest
              })
            } else {
              //选择了结论
              this.subData.relationId.forEach(x => {
                arr.push({
                  type: 42,
                  relationName: x,
                  relationId: this.resultId,
                  suggest: this.subData.suggest
                })
              })
            }
            this.post(this.api.healthMap.addSuggest, {
              jsonData: arr,
              searchType: 4
            }).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('保存成功!');
                this.$parent.getList();
                this.modalClose();
              }
            })

          }
        }
      })
    },
  },
  components: {
    VueNeditorWrap
  }
}
</script>

<style scoped>

</style>
